package com.jic.product.controller.after;

import com.alibaba.fastjson.JSONArray;
import com.jic.common.base.vo.RestResult;
import com.jic.common.redis.RedisClientTemplate;
import com.jic.enums.RedisKey;
import com.jic.product.after.api.ProductCommentApi;
import com.jic.product.model.entity.File;
import com.jic.product.model.entity.ProductComment;
import com.jic.product.service.FileService;
import com.jic.product.service.ProductCommentService;
import com.jic.product.store.request.ProductCommentRequest;
import com.jic.product.store.response.ProductCommentResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author: MJ
 * @date: 2020-01-09
 * @remark: BBC接口 会员收藏列表
 */
@Slf4j
@Api(tags = "H5-评价")
@RestController
@RequestMapping("/feginProductComment")
public class ProductCommentFeginController implements ProductCommentApi {

    @Autowired
    RedisClientTemplate redisClientTemplate;

    @Autowired
    private ProductCommentService productCommentService;

    @Autowired
    private FileService fileService;



    /**
     * BBC接口 获取商品评论数量
     * @return
     */
    @ApiOperation(value = "获取个人中心商品评论数量")
    @ResponseBody
    @PostMapping("/getProductCommentCount")
    @Override
    public RestResult<Long> getProductCommentCount(String token) {
        try {
            String userStr = redisClientTemplate.get(RedisKey.H5_LOGIN_TOKEN_.getName() + token);
            if (StringUtils.isEmpty(userStr)) {
                return RestResult.error("4000","登录失效");
            }
            Map member = JSONArray.parseObject(userStr, Map.class);
            Long memberId = Long.valueOf(member.get("id").toString());
            long count = productCommentService.queryCount(new ProductComment() {{
                setMemberId(memberId);
            }});
            return RestResult.success(count);
        } catch (Exception e) {
            log.error("异常",e);
          return RestResult.error("9999","系统异常");
        }
    }

    @ApiOperation("根据子单编号查询评论")
    @ResponseBody
    @PostMapping("getProductCommentByOrderNo")
    public RestResult<ProductCommentResponse> getProductCommentByOrderNo(@RequestBody ProductCommentRequest productCommentRequest) {
        List<ProductComment> productComments = productCommentService.query(new ProductComment() {{
            setOrderNo(productCommentRequest.getOrderNo());
            setProductId(productCommentRequest.getProductId());
        }});
        List<ProductCommentResponse> resList = new ArrayList<>();
        productComments.forEach(productComment -> {
            ProductCommentResponse productCommentResponse = new ProductCommentResponse();
            BeanUtils.copyProperties(productComment, productCommentResponse);
            List<File> files = fileService.query(new File() {{
                setType(0);
                setBusinessId(productComment.getId());
            }});
            productCommentResponse.setFileList(files);
            resList.add(productCommentResponse);
        });
        Map<Integer, List<ProductCommentResponse>> commentResList = resList.stream().collect(Collectors.groupingBy(productCommentResponse -> productCommentResponse.getType()));
        if (commentResList != null) {
            List<ProductCommentResponse> productCommentResponses = commentResList.get(1);
            if (CollectionUtils.isNotEmpty(productCommentResponses)) {
                ProductCommentResponse productCommentResponse = productCommentResponses.get(0);
                productCommentResponse.setChildren(commentResList.get(2) == null ? new ArrayList<>() : commentResList.get(2));
                return RestResult.success(productCommentResponse);
            }
        }
        ProductCommentResponse productCommentResponse = new ProductCommentResponse();
        productCommentResponse.setChildren(new ArrayList<>());
        return RestResult.success(productCommentResponse);
    }
}
